*******************************************************
* .do-file to estimate marital transition probabilites 
*******************************************************

clear all
set maxvar 7000

use "$psidwkddata/famind_combined_adj_sing.dta", clear

////////////////////////////////////////
/////// SOME VARIABLES ADJUSTMENT //////
////////////////////////////////////////

// generate personal identifier
gen uniqueid_ref = (famid68*1000) + persid if ref    == 1
gen uniqueid_sp  = (famid68*1000) + persid if spouse == 1

gen uniqueid     = uniqueid_ref if ref == 1
replace uniqueid = uniqueid_sp  if spouse == 1

// recode sexind as dummy 
replace sexind = 0 if sexind == 1
replace sexind = 1 if sexind == 2

// create additional variables for regression (from individual perspective)
gen genderage = sexind*ageind
gen ageindsq  = ageind^2

// dummy for being married or not 
gen marital     = 0 if single == 1 | single == 0
replace marital = 1 if couple == 1 | couple == 0

// reverse marital dummy (for better interpretation of divorce prob.)
gen maritalrev     = 1 if single == 1 | single == 0
replace maritalrev = 0 if couple == 1 | couple == 0

// only keep reference person and spouses
keep if uniqueid != .

// dummy for waves after 1997
gen bian = 1 if wave > 1997
replace bian = 0 if bian != 1

// after 1997, PSID switched from annually to bi-annualy
// rename waves to accoomndate stata L.command
replace wave = 1998 if wave == 1999
replace wave = 1999 if wave == 2001
replace wave = 2000 if wave == 2003
replace wave = 2001 if wave == 2005
replace wave = 2002 if wave == 2007
replace wave = 2003 if wave == 2009
replace wave = 2004 if wave == 2011
replace wave = 2005 if wave == 2013
replace wave = 2006 if wave == 2015
replace wave = 2007 if wave == 2017


/////////////////////////////////////////////////
/////// MARRIAGE AND DIVORCE PROBABILITIES //////
/////////////////////////////////////////////////

// SINGLES: marriage probabilies depending on age, gender and education
preserve
keep if ageind >= 30 & ageind <= 65
tsset uniqueid wave
logit f1.marital sexind ageind i.bian i.educind_cat if single != . [pweight = weightind], vce(cluster uniqueid)

outreg2 using "$resultpath/marital.tex", ///
ctitle("marriage prob") replace
restore

// COUPLES: divorce probabilities depending on age and education
preserve
keep if ref == 1
keep if ageref >= 30 & ageref <= 65
tsset uniqueid wave
logit f1.maritalrev ageref i.educcat i.educcatsp i.bian if couple != . [pweight = weightind], vce(cluster uniqueid)

outreg2 using "$resultpath/marital.tex", ///
ctitle("divorce prob") append
restore


//////////////////////////////////////////////////////////
/////// EMPIRICAL ESTIMATE: ASSET DROP UPON DIVORCE //////
//////////////////////////////////////////////////////////

// time series dimension
tsset uniqueid wave

// dummy if individual got divorced from last year to current
gen     ev_div = 1 if L.marital == 1 & marital == 0 
replace ev_div = 0 if L.marital == 1 & marital == 1

// dummy for period before divorce
gen bef_div = 1 if ev_div == 0 & F.ev_div == 1

// asset split upon divorce
gen asset_cp = finwealth if bef_div == 1        // assets of couple in period before divorce
bysort uniqueid: egen asset_bd = max(asset_cp)	

gen asset_s = finwealth if ev_div == 1			// assets of single after divorce
gen asset_frac = asset_s/asset_bd
drop asset_cp asset_s asset_bd

// drop unreasably large changes in financial wealth (+- 1500%) (23 observations)
replace asset_frac = . if asset_frac < -15 | asset_frac > 15

sum asset_frac if single == 0 [aweight = weightind], det  // median: 44%
sum asset_frac if single == 1 [aweight = weightind], det  // median: 44%